package com.oc.mba.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.QuestionSolutionDAO;
import com.oc.mba.entity.OcQuestionSolution;
import com.oc.mba.util.CommLog;

@Repository
public class QuestionSolutionDAOImpl extends BaseDAOHibernate implements QuestionSolutionDAO {
	private static final CommLog logger = new CommLog(QuestionSolutionDAOImpl.class);

	public List<OcQuestionSolution> findByProperty(String propertyName, Object value) {
		logger.debug("finding OcQuestionSolution instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from OcQuestionSolution as model where model."
					+ propertyName + "= ? order by model.id";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public void save(OcQuestionSolution solution) {
		getHibernateTemplate().save(solution);
	}
	
	@Override
	public OcQuestionSolution findQuestionSolution(Integer solutionID) {
		List<OcQuestionSolution> solutionList = findByProperty("id", solutionID);
		if (solutionList != null && solutionList.size() > 0) {
			return solutionList.get(0);
		}
		return null;
	}

	@Override
	public void deleteSolution(Integer solutionID) {
		OcQuestionSolution solution = findQuestionSolution(solutionID);
		if (solution != null) {
			getHibernateTemplate().delete(solution);
		}
	}
	
}
